package cn.ad.server.web.spring.service.httpclient4;

import cn.ad.server.web.spring.service.components.http.client.*;
import cn.ad.server.web.spring.service.components.http.client.AdHttpPostRequest.EntityType;

import java.io.IOException;
import java.util.Map;

/**
 * 包装AdHttpExecutor的实现类
 * @author wangwei
 *
 */
public class HttpClientExecutorUtil implements AdHttpExecutorUtil {

	/**
	 * 具体执行对象。
	 */
    private AdHttpExecutor executor;
	
	@Override
	public AdHttpResponse<ByteArrayWrapStream> doGetExecute(String url, Map<String, Object> headers, int timeout)
			throws ResponseHandleException, IOException {
		AdHttpGetRequest request = new AdHttpGetRequest();
		request.setUrl(url);
		if(headers!=null){
			request.setHeaderParams(headers);
		}
		if(timeout!=-1&&timeout!=0){
			request.setTimeOut(timeout);
		}
		AdResponseHandler<ByteArrayWrapStream> responseHandler = new ByteArrayWrapStreamResponseHandler();
		return executor.execute(request,responseHandler);
	}

	@Override
	public AdHttpResponse<ByteArrayWrapStream> doPostExecute(String url, Map<String, Object> headers,
                                                             Map<String, Object> forms, int timeout) throws ResponseHandleException, IOException {
		AdHttpPostRequest request = new AdHttpPostRequest();
		request.setUrl(url);
		if(headers!=null){
			request.setHeaderParams(headers);
		}
		if(forms!=null){
			request.setParams(forms);
		}
		
		if(timeout!=-1&&timeout!=0){
			request.setTimeOut(timeout);
		}
		request.setType(EntityType.FORM);
		
		AdResponseHandler<ByteArrayWrapStream> responseHandler = new ByteArrayWrapStreamResponseHandler();
		return executor.execute(request,responseHandler);
	}

	@Override
	public AdHttpResponse<ByteArrayWrapStream> doPostExecute(String url, Map<String, Object> headers, String json,
                                                             int timeout) throws ResponseHandleException, IOException {
		AdHttpPostRequest request = new AdHttpPostRequest();
		request.setUrl(url);
		if(headers!=null){
			request.setHeaderParams(headers);
		}
		if(json!=null){
			request.setJson(json);
		}
		
		if(timeout!=-1&&timeout!=0){
			request.setTimeOut(timeout);
		}
		request.setType(EntityType.JSON);
		
		AdResponseHandler<ByteArrayWrapStream> responseHandler = new ByteArrayWrapStreamResponseHandler();
		return executor.execute(request,responseHandler);
	}

	@Override
	public AdHttpResponse<ByteArrayWrapStream> doPostExecute(String url, Map<String, Object> headers, byte[] buffer,
                                                             int timeout) throws ResponseHandleException, IOException {
		AdHttpPostRequest request = new AdHttpPostRequest();
		request.setUrl(url);
		if(headers!=null){
			request.setHeaderParams(headers);
		}
		if(buffer!=null){
			request.setBuffer(buffer);
		}
		
		if(timeout!=-1&&timeout!=0){
			request.setTimeOut(timeout);
		}
		request.setType(EntityType.BYTE);
		
		AdResponseHandler<ByteArrayWrapStream> responseHandler = new ByteArrayWrapStreamResponseHandler();
		return executor.execute(request,responseHandler);
	}

	public AdHttpExecutor getExecutor() {
		return executor;
	}

	public void setExecutor(AdHttpExecutor executor) {
		this.executor = executor;
	}


	
}
